package com.newtouch.bxzs.business.train.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newtouch.bxzs.business.train.mapper.UserCourseMapper;
import com.newtouch.bxzs.business.train.model.bo.CourseBO;
import com.newtouch.bxzs.business.train.model.bo.UserCourseBO;
import com.newtouch.bxzs.business.train.model.bo.UserCoursePageBO;
import com.newtouch.bxzs.business.train.model.vo.UserCoursePageVO;
import com.newtouch.bxzs.business.train.model.vo.UserCourseVO;
import com.newtouch.bxzs.business.train.service.UserCourseService;
import com.newtouch.bxzs.common.base.BaseErrorEnum;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.model.UserCourse;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Objects;

/**
 * 客户课程管理service实现类
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/2/23 9:56</td>
 * <td>dingqianbin</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author dingqianbin
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class UserCourseServiceImpl implements UserCourseService {

    private static final Log logger = LogFactory.getLog(UserCourseServiceImpl.class);

    @Autowired
    UserCourseMapper userCourseMapper;

    @Override
    public UserCourseBO modifyUserCourser(UserCourseVO userCourseVO) {
        /* 检查必传项 */
        if (StringUtils.isEmpty(userCourseVO.getCourseId())) {
            logger.error("增加/修改用户课程信息课程ID不能为空！");
            throw new BaseException(BaseErrorEnum.REQUIRED_ITEM_NULL);
        }
        if (userCourseVO.getCourseDuration() <= 0) {
            logger.error("增加/修改用户课程信息课程时长不能为空！");
            throw new BaseException(BaseErrorEnum.REQUIRED_ITEM_NULL);
        }
        UserCourse userCourse = new UserCourse();
        BeanUtil.copyProperties(userCourseVO, userCourse);
        Date date = new Date();
        String userId = Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId();
        userCourse.setUpdateTime(date);
        userCourse.setUpdateUserId(userId);
        int flag;
        /* 如果用户课程ID不为空，则新增 */
        if (StringUtils.isEmpty(userCourse.getUserCourseId())) {
            userCourse.setCreateUserId(userId);
            userCourse.setCreateTime(date);
            flag = userCourseMapper.insert(userCourse);
        } else {
            flag =  userCourseMapper.update(userCourse, new QueryWrapper<UserCourse>().eq("USER_COURSE_ID", userCourse.getUserCourseId()));
        }
        UserCourseBO userCourseBO = new UserCourseBO();
        if (1 == flag) {
            BeanUtil.copyProperties(userCourse, userCourseBO);
        }
        return userCourseBO;
    }

    @Override
    public UserCoursePageBO getUserCourseList(UserCoursePageVO userCoursePageVO) {
        UserCoursePageBO userCoursePageBO = new UserCoursePageBO();
        Page<CourseBO> page = new Page<>(userCoursePageVO.getCurrentPage(), userCoursePageVO.getPageSize());
        String userId = Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId();
        IPage<CourseBO> courseBOIPage = userCourseMapper.selectUserCourse(page, userCoursePageVO.getIsLearningInsurance(), userId);
        userCoursePageBO.setCourseBOIPage(courseBOIPage);
        if (userCoursePageVO.getIsLearningInsurance() == 1) {
            userCoursePageBO.setUserTodayLearningDuration(userCourseMapper.selectDuration(true, userId));
            userCoursePageBO.setUserTotalLearningDuration(userCourseMapper.selectDuration(false, userId));
        }
        return userCoursePageBO;
    }
}
